package util.xml;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class GetNodes {
	static final String ARCHIVO = "empleados.xml";
	Document doc;
	
	void crearDocumento(){
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(ARCHIVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	void leerElementos(){
		Element raiz = doc.getDocumentElement();
		System.out.println(raiz.getTagName());
		if (raiz.hasChildNodes())
			childNodes(raiz);
	}
	
	void childNodes(Node nodo){
		NodeList nodos = nodo.getChildNodes();
		for (int i = 0; i < nodos.getLength(); i++){
			Node child = nodos.item(i);
			String valor = (child.getNodeType()==Node.TEXT_NODE?child.getNodeValue():child.getNodeName());
			System.out.println(valor);
			if(nodos.item(i).hasChildNodes())
				childNodes(nodos.item(i));
		}
	}
	
	public static void main(String[] args) {
		GetNodes ins = new GetNodes();
		ins.crearDocumento();
		ins.leerElementos();
	}

}
